*************************************************************************
***** Replication Code for:  										 ****
**** Choulis, Ioannis, Abel Escribà-Folch, & Marius Mehrl 			 ****
**** Preventing Dissent: Secret Police And Protests In Dictatorships ****
**** The Journal of Politics 										 ****
**** Date of Code: 15 August 2023 									 ****
*************************************************************************


******************************************************
******************************************************
**** Construct and Merge Dataset, Label Variables ****
******************************************************
******************************************************

* PLEASE NOTE: For replication, please change file paths to reflect where you have stored this code and data. Please also ensure to use the following folder structure:
* Folder 1 (highest level) - Files: Replication_Analysis_SecPolice_Protest.do; Replication_Merge_SecPolice_Protest.do; Replication_secpol_protest; Folder: "Source data"
* Folder 2 (Source data): Files: All remaining .dta, .csv, .xls, and .do files EXCEPT 
* 1: 08_15_23_0925am_wep.csv and varlabel.do, these should be in folder "WB pop and GDP pc" within Source data
* 2: GWF-personalism-measure.do and GWF.dta, these should be in folder "GWF Personalization" within Source data

* Please also create an empty folder "Temp" within Source data


******************************************************
** Merge, starting from raw secret police data file **
******************************************************

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\rawdta_secretpolice.dta", replace
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* GDP & Population Gleditsch - Original data source: http://ksgleditsch.com/exptradegdp.html
import delimited "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\gled_gdppop.csv", clear
replace ccode=679 if ccode==678
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\gled_gdppop.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\gled_gdppop.dta"
drop if _merge==2
drop _merge
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* GDP & Population World bank - Original data source: https://ncgg.princeton.edu/wep/download.html
import delimited "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\WB pop and GDP pc\08_15_23_0925am_wep.csv", clear
do "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\WB pop and GDP pc\varlabel.do"
drop if ccode=="NA"
destring ccode, replace
duplicates drop
replace gdppc_wdi="." if gdppc_wdi=="NA"
replace pop_wdi="." if pop_wdi=="NA"
destring gdppc_wdi pop_wdi, replace
replace ccode=679 if ccode==678
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\wb_gdppop.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\wb_gdppop.dta", keepusing(pop_wdi gdppc_wdi)
drop if _merge==2
drop _merge

* Combine WB and Gleditsch data to construct full GDP p.c. and Population variables
gen pop=pop_wdi
replace pop=pop_gleditsch*1000 if pop_wdi==.
gen gdp_pc=gdppc_wdi
replace gdp_pc=realgdp_gleditsch if gdppc_wdi==.

drop realgdp_gleditsch pop_gleditsch pop_wdi gdppc_wdi

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* UCDP Intrastate Conflict, Version 20.1 - Original Source: https://ucdp.uu.se/downloads/ucdpprio/ucdp-prio-acd-201-dta.zip
use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\ucdp-prio-acd-201.dta", clear
keep if type>=3
gen intrastate=1
collapse (max) intrastate, by(gwno_a year)
rename gwno_a ccode
destring ccode, replace
replace ccode=679 if ccode==678
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\ucdpconflict.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\ucdpconflict.dta", keepusing(intrastate)
drop if _merge==2
drop _merge
replace intrastate=0 if intrastate==.

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Polity data - For unknown reasons, Zimbabwe 1970-79 is excluded from the Polity V data, even though these years were included in polity IV and, according to p.46 of the user manual, should also be included in Polity V. These years are thus manually entered using values from the Polity IV data. Original source: http://www.systemicpeace.org/inscr/p5v2018d.xls
import excel "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\p5v2018.xls", sheet("p5v2018") firstrow clear
replace ccode=816 if ccode==818 & year!=1976
replace ccode=530 if ccode==529 & year!=1993
replace ccode=365 if ccode==364 & year!=1922
replace ccode=345 if ccode==347 & year!=1991
replace ccode=345 if ccode==342 & year!=2006
replace ccode=679 if ccode==678 & year!=1990
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\polity5.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\polity5.dta", keepusing(polity2)
drop if _merge==2
drop _merge

replace polity2=4 if ccode==552 & year>=1970 & year<=1979

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Coup Attempts (Powell & Thyne)
insheet using "http://www.uky.edu/~clthyn2/coup_data/powell_thyne_coups_final.txt", clear
gen attempt=1
replace ccode=679 if ccode==678
collapse (max) attempt, by(ccode year)
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\coups.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\coups.dta", keepusing(attempt)
drop if _merge==2
drop _merge
replace attempt=0 if attempt==. & year>=1950

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Fariss Human Rights Data - original source: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/TADPGE
import delimited "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\HumanRightsProtectionScores_v3.01.csv", clear
rename cow ccode
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\HumanRightsProtectionScores_v3.01.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\HumanRightsProtectionScores_v3.01.dta", keepusing(theta_mean)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* CIRI - Original dataset source: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/UKCPXT
import delimited "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\CIRI Data 1981_2011 2014.04.14(1).csv", clear 
drop if physint==.
tab ctry if cow==.
replace cow=347 if ctry=="Kosovo" & cow==.
replace cow=345 if ctry=="Serbia" & cow==.
replace cow=341 if ctry=="Montenegro" & cow==.
rename cow ccode
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\ciriX.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\ciriX.dta", keepusing(physint disap kill polpris tort)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* State Capacity variables - Original dataset source: http://www-personal.umich.edu/~jkhanson/state_capacity.html
use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\StateCapacityDataset_v1.dta", keepusing(Capacity v2clrspct v2stfisccap v2terr)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* V-Dem Data on CSOs - Original dataset (Entire dataset, which is too large for some Stata licenses, data included here represents subset of variables obtained via "keep" command) source: https://www.v-dem.net/data/the-v-dem-dataset/country-year-v-dem-fullothers-v13/
use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\V-Dem_CSOvariables.dta", keepusing(v2csantimv v2csprtcpt v2csreprss v2cseeorgs v2csstruc_1)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* CHISOLS dummy on regime change - Original dataset source (state year level): http://www.chisols.org/data-sets.html
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\CHISOLSstyr5_0.dta", keepusing(solschdum)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Urban Population and Economic Growth: World Bank, via Escriba-Foch et al. 2018 - Original dataset source: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/TVZQG6 ()
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\temp - Escribaajps.dta", keepusing(urbanpopulationoftotalspurbtotli l12gr)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Personalization: GWF 2018 - Original dataset source: https://sites.psu.edu/wright/data/ (GWF provide data and do-file for IRT model, variable used here results from running do-file on data)
do "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\GWF Personalization\GWF-personalism-measure.do"

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\GWF_personalizationIRT.dta", keepusing(xpers)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Excluded Ethnic Groups: EPR data, RFE Release 2.1 - Original data source: https://growup.ethz.ch/rfe
import delimited "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\eprdata.csv", clear
rename countries_gwid ccode
replace ccode=679 if ccode==678
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\eprdata.dta", replace

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\eprdata.dta", keepusing(lexclpop)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Counterbalancing: Pilster & Bohmelt - Original Data source: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/JCVR1H
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Coup-Proofing 1970-2022.dta", keepusing(effective)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Latent Protest measure: Escribà-Folch, Meseguer, and Wright 2018 - Original data source: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/TVZQG6
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\latentdata.dta", keepusing(mean3 mean5)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace

* Neighbour protest: Computed following Escribà-Folch et al. 2018 (with their protest data) using C-shapes distances
do "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\neighbourProtest.do"

use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear
merge 1:1 ccode year using "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\neighbor_protest.dta", keepusing(nbr_mean3 nbr_mean5)
drop if _merge==2
drop _merge

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace


******************************************
*** Label variables to clarify meaning ***
******************************************
use "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", clear

rename urbanpopulationoftotalspurbtotli urbanpop

label variable secretpol_revised "Secret Police Dummy"
label variable pop "Population Size (WB + Gleditsch)"
label variable gdp_pc "GDP per capita (WB + Gleditsch)"
label variable intrastate "Intraste Conflict (UCDP)"
label variable polity2 "Polity2 score"
label variable attempt "Coup attempt (Powell & Thyne)"
label variable theta_mean "Latent Human Rights Score (Fariss)"
label variable physint "CIRI Physcial Integrity Rights Score"
label variable disap "CIRI Disappearence Score"
label variable kill "CIRI Extrajudicial Killing Score"
label variable polpris "CIRI Political Imprisonment Score"
label variable tort "CIRI Torture Score"
label variable v2terr "Territorial Control (V-Dem)"
label variable solschdum "Source of Leader Support Change Dummy (CHISOLS)"
label variable l12gr "Economic Growth (WB)"
label variable xpers "Latent Personalization (GWF)"
label variable lexclpop "MEG population as fraction of ethnically relevant population (EPR)"
label variable effectivenumber "Effective Number of Military Orgs (Pilster & Bohmelt)"
label variable mean3 "Latent Protest (Poisson IRT)"
label variable mean5 "Latent Protest (NB IRT)"
label variable nbr_mean3 "Latent Neighbor Protest (Poisson IRT)"
label variable nbr_mean5 "Latent Neighbor Protest (NB IRT)"

save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Source data\Temp\Reconstruct_secpol_protest.dta", replace
     
save "C:\Users\mariu\Dropbox\Secret Police Protest\Data\Replication\Replication_secpol_protest.dta", replace

